import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { sysMRegion } from "@/api/backend/index";
import { handleTree, handleRegionIds, getLastRegionId } from "@/utils/common";
import { Local } from "@/utils/storage";

type Region = {
  code: number;
  msg: string;
  rows: any;
  total: number;
};
export const useRegion = defineStore(Names.region, {
  state: () => {
    return {
      //用户的权限区域树列表 断言:<any>[] 、[] as any
      userRegionByRole: <any>[],
      regionIds: [],
      originData: <any>[], //未处理过后的区域列表
      usrRegionIds: <any>[],
    };
  },
  getters: {},
  actions: {
    //获取区域管理数据并处理
    getRegionList() {
      return new Promise((resolve, reject) => {
        _get(
          sysMRegion + "/list",
          {
            pageNum: 1,
            pageSize: 10000,
          },
          "json",
          ""
        )
          .then((res: any | Region) => {
            let data = handleTree(res.rows, "areaId", "", "");
            console.log("处理过后的区域列表", data);
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //权限查询区域树
    getRegionByRole() {
      return new Promise((resolve, reject) => {
        _get(sysMRegion + "/treeList", {}, "json", "")
          .then(async (res: any | Region) => {
            this.originData = res.rows; //原始树形结构数据
            let data = handleTree(res.rows, "areaId", "", ""); //处理成el-tree接受的树型数据
            let Ids = await handleRegionIds(res.rows);
            this.userRegionByRole = data;
            (this.regionIds as Array<number>) = Ids as Array<number>;
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //获取最底层区域id
    getLastAreaId(data: any, parentId: number) {
      return new Promise(async (resolve, reject) => {
        for (let i = 0, len = data.length; i < len; i++) {
          if (data[i].areaId === parentId) {
            let filter = await getLastRegionId(data[i].children);
            resolve(filter);
          }
        }
      });
    },
    //获取
    getChildren(id: number) {
      return new Promise((resolve, reject) => {
        _get(
          sysMRegion + "/list",
          { pageSize: 100, parentId: id },
          "json",
          ""
        ).then((res: any) => {
          console.log("res");

          let ids: any[] = [];
          for (let i = 0; i < res.rows.length; i++) {
            ids.push(res.rows[i].areaId);
          }
          console.log("区域子级", ids);
          resolve(ids);
        });
      });
    },
    //
    getName(id: any) {
      return new Promise((resolve, reject) => {
        _get(sysMRegion + "/" + id, {}, "json", "").then((res: any) => {
          resolve(res.data.areaName);
        });
      });
    },
    // getInfo(name: string, value: any) {
    //   return new Promise((resolve, reject) => {
    //     switch (name) {
    //       case "areaName":
    //         _get(sysMRegion, { areaName: value }, "json", "").then(
    //           (res: any) => {
    //             resolve(res.data.areaId);
    //           }
    //         );
    //         break;
    //       case "areaId":
    //         _get(sysMRegion + "/" + value, {}, "json", "").then((res: any) => {
    //           resolve(res.data.areaName);
    //         });
    //         break;
    //       default:
    //         break;
    //     }
    //   });
    // },
  },
});
